package com.achievement.project.system.choose.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.achievement.project.system.course.domain.Course;
import com.achievement.project.system.score.domain.Score;
import com.achievement.project.system.score.mapper.ScoreMapper;
import com.achievement.project.system.student.domain.Student;
import com.achievement.project.system.student.mapper.StudentMapper;
import com.achievement.project.system.user.domain.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.achievement.project.system.choose.mapper.CourseChooseMapper;
import com.achievement.project.system.choose.domain.CourseChoose;
import com.achievement.project.system.choose.service.ICourseChooseService;
import com.achievement.common.utils.text.Convert;

/**
 * 课程管理Service业务层处理
 *
 * @author zkai
 * @date 2021-04-27
 */
@Service
public class CourseChooseServiceImpl implements ICourseChooseService
{
    @Autowired
    private CourseChooseMapper courseChooseMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ScoreMapper scoreMapper;

    /**
     * 查询课程管理
     *
     * @param courseId 课程管理ID
     * @return 课程管理
     */
    @Override
    public CourseChoose selectCourseChooseById(Integer courseId)
    {
        return courseChooseMapper.selectCourseChooseById(courseId);
    }

    /**
     * 查询课程管理列表
     *
     * @param courseChoose 课程管理
     * @return 课程管理
     */
    @Override
    public List<CourseChoose> selectCourseChooseList(CourseChoose courseChoose)
    {
        return courseChooseMapper.selectCourseChooseList(courseChoose);
    }

    /**
     * 新增课程管理
     *
     * @param courseChoose 课程管理
     * @return 结果
     */
    @Override
    public int insertCourseChoose(CourseChoose courseChoose)
    {
        return courseChooseMapper.insertCourseChoose(courseChoose);
    }

    /**
     * 修改课程管理
     *
     * @param courseChoose 课程管理
     * @return 结果
     */
    @Override
    public int updateCourseChoose(CourseChoose courseChoose)
    {
        return courseChooseMapper.updateCourseChoose(courseChoose);
    }

    /**
     * 删除课程管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCourseChooseByIds(String ids)
    {
        return courseChooseMapper.deleteCourseChooseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程管理信息
     *
     * @param courseId 课程管理ID
     * @return 结果
     */
    @Override
    public int deleteCourseChooseById(Integer courseId)
    {
        return courseChooseMapper.deleteCourseChooseById(courseId);
    }

    @Override
    public int insertCourseBySid(Integer courseId, String sIds) {
        //将学生id字符串转换Long[] 数组存放
        Long[] studentIds = Convert.toLongArray(sIds);
        CourseChoose courseChoose = courseChooseMapper.selectCourseChooseById(courseId);
        // 新增用户与角色管理
        List<Course> list = new ArrayList<Course>();
        List<Score> scoreList = new ArrayList<Score>();
        for (Long sId : studentIds){
            Course course = new Course();
            course.setCourseId(Long.valueOf(courseId));
            course.setcTypeId(Long.valueOf(courseChoose.getCourseType()));
            course.setsId(sId);
            course.setpId(courseChoose.getTeacherId());
            course.setSemester(courseChoose.getSemester());
            list.add(course);

            Score score = new Score();
            Student student = studentMapper.selectStudentById(sId);
            score.setStudentId(sId);
            score.setSno(student.getNo());
            score.setSname(student.getName());
            score.setGradeId(String.valueOf(student.getGradeId()));
            score.setProfessionalId(String.valueOf(student.getProfessionalId()));
            score.setClassId(String.valueOf(student.getClassId()));
            score.setCourseId(Long.valueOf(courseId));
            score.setcTypeId(Long.valueOf(courseChoose.getCourseType()));
            score.setTeacherId(Long.valueOf(courseChoose.getTeacherId()));
            score.setSemester(student.getSemester());
            scoreMapper.insertScore(score);
        }



        return courseChooseMapper.batchCourseStudent(list);
    }

    @Override
    public int deleteCourseStudentOne(Integer courseId, Integer sId) {
        scoreMapper.deleteScoreByCourse(Long.valueOf(sId),Long.valueOf(courseId));
        return courseChooseMapper.deleteCourseStudentOne(courseId,sId);
    }

    @Override
    public int deleteCourseStudentAll(Integer courseId, String sIds) {
        scoreMapper.deleteCourseStudentAll(courseId,Convert.toLongArray(sIds));
        return courseChooseMapper.deleteCourseStudentAll(courseId,Convert.toLongArray(sIds));
    }


}
